package com.llt.bottomnavigationf;

import com.llt.bottomnavigationf.entity.AppBar;
import com.llt.bottomnavigationf.entity.Body;
import com.llt.bottomnavigationf.entity.BottomNavItem;
import com.llt.bottomnavigationf.entity.BottomNavigation;
import com.llt.bottomnavigationf.fraction.FractionUtil;
import com.llt.bottomnavigationf.util.LogUtil;

import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

import java.util.ArrayList;
import java.util.List;

public class Scaffold  {
    public OnItemClickListener onItemClickListener;//底部导航监听
    public OnNavClickListener onNavClickListener;
    public OnGetFractionUtil onGetFractionUtil;
    public FractionUtil fractionUtil;
    private FractionAbility abilitySlice;
    private AppBar appBar;//顶部导航
    private Body body;//内容体
    private BottomNavigation bottomNavigationBar;//底部导航蓝
    private Component parse;//scaffold xml 体
    private int currentSaveIndex = 0;//当前激活按钮索引值
    public Scaffold(FractionAbility abilitySlice) {
        this.abilitySlice = abilitySlice;
    }
    public Scaffold(FractionAbility abilitySlice, AppBar appBar) {
        this.abilitySlice = abilitySlice;
        this.appBar = appBar;
    }
    public Scaffold(FractionAbility abilitySlice,AppBar appBar, BottomNavigation bottomNavigationBar) {
        this.abilitySlice = abilitySlice;
        this.appBar = appBar;
        this.bottomNavigationBar = bottomNavigationBar;
    }
    public Scaffold(FractionAbility abilitySlice, Body body, BottomNavigation bottomNavigationBar) {
        this.abilitySlice = abilitySlice;
        this.body = body;
        this.bottomNavigationBar = bottomNavigationBar;
    }
    public Scaffold(FractionAbility abilitySlice, AppBar appBar, Body body, BottomNavigation bottomNavigationBar) {
        this.abilitySlice = abilitySlice;
        this.appBar = appBar;
        LogUtil.info("appBar", Integer.toString(appBar.getLeadingId()));
        this.body = body;
        this.bottomNavigationBar = bottomNavigationBar;
    }

    /**
     * 获取 FractionUtil
     * @param onGetFractionUtil
     */
    public void getGetFractionUtil(OnGetFractionUtil onGetFractionUtil ){
        this.onGetFractionUtil = onGetFractionUtil;
    }
    /**
     * 获取监听 导航点击事件
     * @param listener
     */
    public void setOnNavClickListener(OnNavClickListener listener){
        this.onNavClickListener = listener;
    }
    /**
     * 底部导航点击事件
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener){
            this.onItemClickListener = listener;
    }

    /**
     * 获取swiptch 工具类
     * @return
     */
    public FractionUtil getFractionUtil(){
        return this.fractionUtil;
    }
    /**
     * 获取swiptch 工具类
     * @return
     */
    public void setFractionUtil(FractionUtil fractionUtil){
         this.fractionUtil=fractionUtil;
    }
    /**
     * 初始化 导航 body 以及底部导航
     * @return
     */
    public Scaffold init() {
        /**
         * 初始化 数据 fraction
         */
        abilitySlice.getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                fractionUtil=new FractionUtil(
                        body.getFractions(),
                        currentSaveIndex,
                        abilitySlice,
                        ResourceTable.Id_scaffold_main_content);
                onGetFractionUtil.getFractionUtil(fractionUtil);
            }
        });


        /**
         * parse xmlId 父级组件根组件 是否依赖到夫组件上 false 不依赖
         * scaffold xml 体
         */
         parse = LayoutScatter.getInstance(abilitySlice.getContext())
                .parse(ResourceTable.Layout_Scaffold_main, null, false);
        initNav();
        //底部导航 start
        if (bottomNavigationBar!=null){
            initBottom();
        }else {
            throw new NullPointerException("bottomNavigationBar底部导航没数据");
        }
        //底部导航 end
        return this;
    }
    public void initNav(){
        Image image=null;
        /**
         * scaffold 内的id
         */
        DirectionalLayout scaffold_nav_top_bar_component =(DirectionalLayout) parse.findComponentById(ResourceTable.Id_scaffold_nav_top_bar);
        //true 是 自定义导航布局 false 走默认
        if (appBar!=null){
            /**
             * 导航布局xml
             */
            Component  _appBar = LayoutScatter.getInstance(abilitySlice.getContext())
                    .parse(ResourceTable.Layout_nav_top_bar, null, false);
            scaffold_nav_top_bar_component.addComponent(_appBar);
            if(appBar.getTitle()=="")  throw new NullPointerException("appBar Title 必须有标题");
            image =(Image) _appBar.findComponentById(ResourceTable.Id_nav_top_bar_leading);
            if (appBar.getLeadingId()==0){
                //说明隐藏图标
                image.setVisibility(1);
            }else {
                image.setPixelMap(appBar.getLeadingId());
                // 有图标绑定点击事件
                image.setClickedListener(component -> {
                        onNavClickListener.onLeading();//绑定点击事件传递外部
                });
            }
            Text nav_top_bar_title =(Text) _appBar.findComponentById(ResourceTable.Id_nav_top_bar_title);
            nav_top_bar_title.setText(appBar.getTitle());
            //处理导航leading 点击事件
        }
    }
    public void initBottom(){
        /**
         * scaffold 内的id
         */
        DirectionalLayout scaffold_bottom_tabMenu_component =(DirectionalLayout)
                parse.findComponentById(ResourceTable.Id_scaffold_bottom_tabMenu);
        /**
         * 导航布局xml
         */
        //底部传递进来的数据
        List<BottomNavItem> bottomNavItems = bottomNavigationBar.getBottomNavItems();
        //创建底部容器数据数组
        List<Component> tabList = new ArrayList<>();
//        scaffold_bottom_tabMenu_component.setTotalWeight(bottomNavItems.size());
        for (int i = 0; i <bottomNavItems.size() ; i++) {
            //获取主页面底部容器ID
            Component _bottom_nav_tab =  LayoutScatter.getInstance(abilitySlice.getContext())

                    .parse(ResourceTable.Layout_bottom_nav_tab, null, false);
            //因为导入文件所以必须 重新设置layout 布局
            _bottom_nav_tab.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                    ComponentContainer.LayoutConfig.MATCH_CONTENT,
                    ComponentContainer.LayoutConfig.MATCH_PARENT,
                    LayoutAlignment.CENTER,
                    1
            ));
            //获取模板容器
            tabList.add(_bottom_nav_tab);
            //获取模板容器组件
            Image buttonImage = (Image) _bottom_nav_tab.findComponentById(ResourceTable.Id_bottom_tab_button_img);
            Text buttonText = (Text) _bottom_nav_tab.findComponentById(ResourceTable.Id_bottom_tab_button_text);
            buttonImage.setScaleMode(Image.ScaleMode.INSIDE);
            if (buttonImage != null) {//
                if (i == currentSaveIndex) {
                    buttonImage.setPixelMap(bottomNavItems.get(i).getSelectImg());
                } else {
                    buttonImage.setPixelMap(bottomNavItems.get(i).getImg());
                }
            }
            if (buttonText != null) {
                buttonText.setText(bottomNavItems.get(i).getTitle());
                if (i == this.currentSaveIndex) {
                    int rgb = Color.getIntColor(bottomNavItems.get(i).getSelectColor());
                    Color color = new Color(rgb);
                    buttonText.setTextColor(color);
                } else {
                    int rgb = Color.getIntColor(bottomNavItems.get(i).getUnselectColor());
                    Color color = new Color(rgb);
                    buttonText.setTextColor(color);
                }

            }
            //绑定点击事件
            bottomNavBarClick(_bottom_nav_tab,i,tabList);
            scaffold_bottom_tabMenu_component.addComponent(_bottom_nav_tab);
        }


    }

    /**
     * 按钮点击事件
     * @param item 当前按钮
     * @param index 当前激活索引值

     */
    public void bottomNavBarClick(Component item, int index,List<Component> tabList){
        //底部传递进来的数据
        List<BottomNavItem> bottomNavItems = bottomNavigationBar.getBottomNavItems();
        item.setClickedListener(component -> {
            fractionUtil.show(index);
            onItemClickListener.onTap(index);
            for (int i = 0; i <bottomNavItems.size() ; i++) {
                Image buttonImage = (Image) tabList.get(i).findComponentById(ResourceTable.Id_bottom_tab_button_img);
                Text buttonText = (Text) tabList.get(i).findComponentById(ResourceTable.Id_bottom_tab_button_text);
                if (buttonImage != null&&buttonText != null) {//
                    if (i == index) {
                        buttonImage.setPixelMap(bottomNavItems.get(i).getSelectImg());
                        int rgb = Color.getIntColor(bottomNavItems.get(i).getSelectColor());
                        Color color = new Color(rgb);
                        buttonText.setTextColor(color);
                    } else {
                        buttonImage.setPixelMap(bottomNavItems.get(i).getImg());
                        int rgb = Color.getIntColor(bottomNavItems.get(i).getUnselectColor());
                        Color color = new Color(rgb);
                        buttonText.setTextColor(color);
                    }
                }
            }
        });

    }
    public Component getScaffold(){
        return parse;
    }


}
